<?php
/**
 * 初始化全局
 * @authors nideer (netbite@163.com)
 * @date    2017-04-23 08:37:04
 * @link    php.nideer.com
 * @Copyright $Id$
 */
namespace Home\Controller;
use Think\Controller;

class BaseController extends Controller {
  public $Version;
  public $System;
  public $Say_c;
  public $Article;
  public $Link;
  public $Album;
  public $Gust;
  public $Say;
  public $picture;
  public $Album_c;
  public $Article_c;

	/*
  * 初始化操作
  */
	public function _initialize(){

		$this->assign('action',ACTION_NAME);//操作方法名标签位
    //过滤不需要登陆的行为
		if(in_array(ACTION_NAME,array('login','logout','vertify')) || in_array(CONTROLLER_NAME,array('Ueditor','Uploadify'))){
        	//return;
		}else{
		//    if(session('admin_id') > 0 ){
   //      		$this->check_priv();//检查管理员菜单操作权限
   //      	}else{
   //      		$this->error('请先登陆',U('Admin/login'),1);
   //      	}
    }

    $this->public_assign();
  }

  /**
   * 保存公共变量到 smarty中 比如 导航
   */
  public function public_assign()
  {
   $this->Article   = D('article');
   $this->Tag       = D('tag');
   $this->System    = D('system');
   $this->Version   = D('version');
   $this->Say_c     = D('say_c');
   $this->Article_c = D('article_c');
   $this->Album_c   = D('album_c');
   $this->Say       = D('say');
   $this->Album     = D('album');
   $this->picture   = D('picture');
   $this->Link      = D('link');
   $this->Gust      = D('gust');

  $thblog_system = array();
  $thblog_system['config']['remark'] = C('REMARK');//
  $thblog_system['config']['name'] = C('NAME');
  $this->assign('thblog_system', $thblog_system);

  //--------导航--------
  if(!$pid = S('pid')){
    $pid =  $tag = $this->Tag->field('t_id, t_name, parent_id, t_view,t_root,t_remark')
    ->where("t_view=1 AND parent_id=0")
    //->fetchSql(true)
    ->select();
    foreach ($pid AS $row){
      $cat_arr[$row['t_id']]['t_id']   = $row['t_id'];
      $cat_arr[$row['t_id']]['t_name'] = $row['t_name'];
      $cat_arr[$row['t_id']]['t_remark'] = $row['t_remark'];
      if (isset($row['t_id']) != NULL){           //子目录待完善
        //$cat_arr[$row['t_id']]['child'] = $this->get_child_tree($tag,$row['t_id'],$children);
        $cat_arr[$row['t_id']]['child'] = 1;
      }
    }
  }

  $pid =$cat_arr;
  setS("pid",$pid);
  $this->assign('pid',$pid);//=$this->pid = $pid;

  if(!$system = S('system')){
    $system = $this->System->find();
    setS('system',$system);
  }
  $this->system = $system;

  if(!$version = S('version')){
    $version = $this->Version->order('v_id desc')->limit(1)->getField('v_version');
    setS("version", $version);
  }
  $this->version = $version;

  // 底部随机推荐5篇文章 不足5篇为空
  if(!$f_article = S('f_article')){
    $tmp = $this->Article -> where(array('a_view'=>2))->getField('a_id',true);
    $rt = array_rand($tmp,5);
    $f_article = array();
    for($i=0;$i<5;$i++){
      $f_article[] = $this->Article->where(array("a_id"=>$tmp[$rt[$i]]))->find();
    }
    setS("f_article", $f_article);
  }
  $this->assign('f_article',$f_article);

  if(!$num = S('num')){
    $say_n = $this->Say_c-> where("sc_rtime != 0")->count();
    $article_n = $this->Article_c->where("ac_rtime != 0")->count();
    $album_n = $this->Album_c->where("alc_rtime != 0")->count();
    $num = array(
      'say'       => $this->Say->where(array('s_view >'=>1))->count(),
      'article'   => $this->Article->where(array('a_view >'=>1))->count(),
      'comment'   => $say_n+$article_n+$album_n,
      'gust'      => $this->Gust->where("g_rtime != 0")->count(),
      'album'   => $this->Album->where(array("al_view"=>1))->count(),
      'picture' => $this->picture->where(array("p_view"=>1))->count(),
      'link'    => $this->Link->where("l_view != 0")->count(),
      );
    setS("num",$num);
  }
  $this->assign("num",$num);

  if(!$album = S('album')){
    $album = $this->Album->where(array('al_view'=>1))->limit(9)->select();
    setS("album", $album);
  }
  $this->foot_album=$album;

  //-----------------------右侧-------------
  if(!$tag = S('tag')){
    $tag = $this->Article
    ->where("a_keyword != '' and a_view > 0")
    ->field('a_keyword,a_id,a_time')
    ->order('a_time desc')
    ->select();
    for($i=1;$i<=count($tag);$i++){
      $tag[$i-1]['key']=$i;
    }
    setS('tag',$tag);
  }
  $this->assign('tag',$tag);

  if(!$s_article = S('s_article')){
    $arr = $this->Article->where('a_view != 0')->getField('a_id',true);
    $res = array_rand($arr,3);
    for($j=0;$j<3;$j++){
      $s_article[] = $this->Article->where(array('a_id'=>$arr[$res[$j]]))->find();
    }
    setS("s_article",$s_article);
  }
  $this->assign("s_article",$s_article);

    if(!$link=S('link')){
      $link = $this->Link->where(array("l_view"=>2))->order('l_sort')->select();
      setS('link',$link);
    }
  $this->assign("link",$link);

  S('s_content',null);
  if(!$s_content = S('s_content')){
    $time = $this->Article_c->where('ac_rtime>0')->field('ac_time')
    ->table('__ARTICLE_C__')
    ->union(array('SELECT sc_time FROM th_say_c WHERE sc_rtime != 0','SELECT alc_time FROM th_album_c WHERE alc_rtime >0 order by ac_time desc limit 0,5'),ture)
    //->fetchSql(true)
    ->select();

    for($i=0;$i<5;$i++){
      $a  = $this->Article_c->where(array('ac_time'=>$time[$i]['ac_time']))->find();
      $al = $this->Album_c->where(array('alc_time'=>$time[$i]['ac_time']))->find();
      $s  = $this->Say_c->where(array('sc_time'=>$time[$i]['ac_time']))->find();
      if($a!=''){$s_content[] = $a;}elseif($al!=''){$s_content[] = $al;}elseif($s!=''){$s_content[] = $s;}
    }
    setS('s_content',$s_content);
  }
  $this->assign('s_content',$s_content);

  if(!$gusts = S('gusts')){
    $gusts = $this->Gust->where('g_rtime>0')->order('g_time desc')->limit(5)->select();
    setS("gusts", $gusts);
  }
  $this->gusts = $gusts;

  if(!$hits = S('hits')){
    $hits = $this->Article->where("a_view != 0")->order('a_hit desc')->limit(7)->select();
    setS('hits',$hits);
  }
  $this->hits = $hits;

  if(!$Time = S('time')){
    $time1 = $this->Article->order('a_time desc')->getField('a_time');
    $time2 = $this->Say->order('s_time desc')->getField('s_time');
    $time3 = $this->Album->order('al_time desc')->getField('al_time');
    $Time = max($time3,max($time1,$time2));
    switch ($Time) {
      case $time1:$msg = '文章';break;
      case $time2:$msg = '说说';break;
      case $time3:$msg = '相册';break;
      default:$msg = 'Error';break;
    }
    setS('time',$Time);
    setS('msg',$msg);
  }
  $this->msg = S('msg');
  $this->time=$Time;
}
///////////////////////////////

public function get_child_tree($tag,$tree_id = 0,$children = 0)
 {
  $three_arr = array();
  $child = $tag->field('t_id, t_name, parent_id, t_view,t_root,t_remark')
  ->where("parent_id=$tree_id AND t_view=1")
  ->limit($children)
  //->fetchSql(true)
  ->select();
  if ($child || $tree_id == 0){
    foreach ($child AS $row){
      $three_arr[$row['t_id']]['t_id']   = $row['t_id'];
      $three_arr[$row['t_id']]['t_name'] = $row['t_name'];
      $cat_arr[$row['t_id']]['t_remark'] = $row['t_remark'];

      if (isset($row['t_id']) != NULL){
        $three_arr[$row['t_id']]['child'] = $this->get_child_tree($tag,$row['t_id'],$children);
      }
    }

  }

    return $three_arr;
   }






  /*
  检测登录权限
   */
  public function check_priv()
  {
  	$ctl = CONTROLLER_NAME;
  	$act = ACTION_NAME;
    $act_list = session('act_list');
	//无需验证的操作
	// $uneed_check = array('login','logout','vertifyHandle','vertify','imageUp','upload','login_task');
//   	if($ctl == 'Index' || $act_list == 'all'){
//   		//后台首页控制器无需验证,超级管理员无需验证
//   		return true;
//   	}elseif(strpos('ajax',$act) || in_array($act,$uneed_check)){
//   		//所有ajax请求不需要验证权限
//   		return true;
//   	}else{
//   		$right = M('system_menu')->where("id in ($act_list)")->cache(true)->getField('right',true);
//   		foreach ($right as $val){
//   			$role_right .= $val.',';
//   		}
//   		$role_right = explode(',', $role_right);
//   		//检查是否拥有此操作权限
//   		if(!in_array($ctl.'Controller@'.$act, $role_right)){
//   			$this->error('您没有操作权限,请联系超级管理员分配权限',U('Admin/Index/welcome'));
//   		}
//   	}
    return true;
  }


}
